package com.projectmodel.rocketmq.service;

import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.kafka.support.SendResult;
import org.springframework.stereotype.Component;
import org.springframework.util.concurrent.ListenableFuture;
import org.springframework.util.concurrent.ListenableFutureCallback;

import javax.annotation.Resource;

@Component
public class KafkaProducerService {

    @Resource
    private KafkaTemplate<String,Object> kafkaTemplate;

    public void sendMessage(String topic,String key,Object object){
        ListenableFuture<SendResult<String,Object>> future = kafkaTemplate.send(topic,0,key,object);
        future.addCallback(new ListenableFutureCallback<SendResult<String, Object>>() {
            @Override
            public void onFailure(Throwable throwable) {
                System.out.println("********消息发送成功："+throwable.toString());

            }
            @Override
            public void onSuccess(SendResult<String, Object> result) {
                System.out.println("=========消息发送成功："+result.toString());
            }
        });
    }
}